package com.jason.redCherry.action;


import com.alibaba.fastjson.JSON;
import com.jason.redCherry.common.Global;
import com.jason.redCherry.common.StatusCodeManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * * <b>类名称：</b>BasicAction<br/>
 * <b>类描述：</b>action基类<br/>
 * <b>创建人：</b>Jason<br/>
 * <b>修改人：</b>Jason<br/>
 *
 * @version 1.0.0<br/>
 */
@SuppressWarnings("serial")
public class BasicAction extends ActionSupport {

    private HttpServletRequest request = ServletActionContext.getRequest();

    private HttpServletResponse response = ServletActionContext.getResponse();

    public Object getAttr(String key) {
        return request.getParameter(key);
    }

    public void setAttr(String key, Object value) {
        ActionContext.getContext().put(key, value);
    }

    public void setSession(String key, Object val) {
        request.getSession().setAttribute(key, val);
    }

    public Object getSession(String key) {

        return request.getSession().getAttribute(key);
    }

    public void clearCurrentUser() {
        request.getSession().removeAttribute(Global.SESSION_CURRENT_USER);
    }

    public void resultJson(String json) {
        response.setContentType("text/json;charset=utf-8");
        try {
            response.getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void resultMsg(String statusCode) {
        this.resultMsg(statusCode, new HashMap<String, Object>());
    }

    public void resultMsg(String statusCode, Object ext) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", statusCode);
        map.put("msg", StatusCodeManager.getCodeMsg(statusCode));
        if (ext != null) {
            map.put("ext", ext);
        }
        resultJson(JSON.toJSONString(map));
    }

    public void alert(String msg) {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        String script = "<script>alert('" + msg + "');</script>";

        resultJson(script);
    }

    public void redirect(String url) {
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void redirect(String url, String para) {

        if (url.contains("?")) {
            url += "&" + para;
        } else {
            url += "?" + para;
        }

        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***************** getter&&setter *******************/

    /***************** self method *******************/

}
